﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.MEP
{
	public class PipeConfigurationManagement
	{
		public static int GetSprinklerDiameter(FamilySymbol sprSym)
		{
			int num = 0;
			try
			{
				if (sprSym == null)
				{
					return num;
				}
				string familyName = sprSym.FamilyName;
				int num2 = familyName.IndexOf("R");
				if (num2 > 0)
				{
					num = Convert.ToInt32(familyName.Substring(num2 + 1));
					num *= 2;
				}
			}
			catch
			{
			}
			return num;
		}

		public static Dictionary<int, IList<RoutingPreferenceRule>> GetRules(int nPipeType, Document doc, bool bNewTran)
		{
			return PipeConfigurationManagement.GetRules(YJKElementFilter.GetElementByClass(typeof(PipeType), doc)[nPipeType] as PipeType, doc, bNewTran);
		}

		public static Dictionary<int, IList<RoutingPreferenceRule>> GetRules(PipeType pipeType, Document doc, bool bNewTran)
		{
			Transaction transaction = null;
			Dictionary<int, IList<RoutingPreferenceRule>> result;
			try
			{
				if (bNewTran)
				{
					transaction = new Transaction(doc, "获得及替换管件");
					transaction.Start();
				}
				Dictionary<int, IList<RoutingPreferenceRule>> dictionary = new Dictionary<int, IList<RoutingPreferenceRule>>();
				IList<RoutingPreferenceRule> segmentRuleFitting = UnifiedModified.GetSegmentRuleFitting(pipeType, 1);
				dictionary.Add(1, segmentRuleFitting);
				UnifiedModified.ClearSegmentFitting(pipeType, 1);
				UnifiedModified.SetSegmentFitting(pipeType, 1, PipeConfigurationManagement.GetFittingFamilySymbol(doc, 1), "盈建科弯头");
				segmentRuleFitting = UnifiedModified.GetSegmentRuleFitting(pipeType, 2);
				dictionary.Add(2, segmentRuleFitting);
				UnifiedModified.ClearSegmentFitting(pipeType, 2);
				UnifiedModified.SetSegmentFitting(pipeType, 2, PipeConfigurationManagement.GetFittingFamilySymbol(doc, 2), "盈建科三通");
				if (bNewTran)
				{
					transaction.Commit();
				}
				result = dictionary;
			}
			catch
			{
				result = null;
			}
			finally
			{
                if (bNewTran && transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private static ElementId GetFittingFamilySymbol(Document doc, int nType)
		{
			ElementId result;
			try
			{
				string text;
				if (nType == 1)
				{
					text = "弯头_空白";
				}
				else
				{
					text = "变径三通_空白";
				}
				string text2 = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "Fitting", text + ".rfa");
				doc.LoadFamily(text2);
				result = YJKRevitTools.GetFamilySymbol(text, doc).Id;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public static void SetRules(int nPipeType, Document doc, Dictionary<int, IList<RoutingPreferenceRule>> segmentRules, bool bNewTran)
		{
			try
			{
				PipeConfigurationManagement.SetRules(YJKElementFilter.GetElementByClass(typeof(PipeType), doc)[nPipeType] as PipeType, doc, segmentRules, bNewTran);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		public static void SetRules(PipeType pipeType, Document doc, Dictionary<int, IList<RoutingPreferenceRule>> segmentRules, bool bNewTran)
		{
			Transaction transaction = null;
			try
			{
				if (bNewTran)
				{
					transaction = new Transaction(doc, "返回管件");
					transaction.Start();
				}
				foreach (KeyValuePair<int, IList<RoutingPreferenceRule>> keyValuePair in segmentRules)
				{
					UnifiedModified.ClearSegmentFitting(pipeType, keyValuePair.Key);
					UnifiedModified.SetSegmentRuleFitting(pipeType, keyValuePair.Key, keyValuePair.Value);
				}
				if (bNewTran)
				{
					transaction.Commit();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
			finally
			{
                if (bNewTran && transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
				{
					transaction.RollBack();
				}
			}
		}

		public static void SetEmptyLength(double dLen, Document doc, ICollection<ElementId> fittings, bool bNewTran)
		{
			Transaction transaction = null;
			try
			{
				if (bNewTran)
				{
					transaction = new Transaction(doc, "返回管件");
					transaction.Start();
				}
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				foreach (ElementId elementId in fittings)
				{
					Element elementById = doc.GetElementById(elementId);
					if (elementById is FamilyInstance)
					{
						FamilyInstance familyInstance = elementById as FamilyInstance;
						if (familyInstance != null && familyInstance.MEPModel != null && familyInstance.MEPModel is MechanicalFitting)
						{
                            if ((familyInstance.MEPModel as MechanicalFitting).PartType == (PartType)6)
							{
								Parameter parameter = familyInstance.GetParameter("空白区域长度");
								if (parameter != null && !parameter.IsReadOnly)
								{
									parameter.Set(dLen / 304.8);
									Parameter parameter2 = familyInstance.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY);
									if (parameter2 != null && !parameter2.IsReadOnly)
									{
										parameter2.Set(0);
										PipeConfigurationManagement.SetTranteAnnotations(familyInstance);
									}
								}
								hyrotateFamilyInstance.MoveFamilyInstance(familyInstance, new XYZ(0.0, 1.0, 0.0));
							}
                            else if ((familyInstance.MEPModel as MechanicalFitting).PartType == (PartType)5)
							{
								Parameter parameter3 = familyInstance.GetParameter("空白区域长度");
								if (parameter3 != null && !parameter3.IsReadOnly)
								{
									parameter3.Set(dLen / 609.6);
									Parameter parameter4 = familyInstance.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY);
									if (parameter4 != null && !parameter4.IsReadOnly)
									{
										parameter4.Set(0);
										PipeConfigurationManagement.SetTranteAnnotations(familyInstance);
									}
								}
								hyrotateFamilyInstance.MoveFamilyInstance(familyInstance, new XYZ(0.0, 1.0, 0.0));
							}
						}
					}
				}
				if (bNewTran)
				{
					doc.Regenerate();
					transaction.Commit();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
			finally
			{
                if (bNewTran && transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
				{
					transaction.RollBack();
				}
			}
		}

		public static void SetTranteAnnotations(FamilyInstance fin)
		{
			try
			{
				foreach (object obj in fin.MEPModel.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					if (connector.IsConnected)
					{
						Connector connectConnector = PipeConfigurationManagement.GetConnectConnector(connector);
						if (connectConnector != null)
						{
							Element owner = connectConnector.Owner;
                            if (owner != null && owner is FamilyInstance && (owner as FamilyInstance).MEPModel != null && (owner as FamilyInstance).MEPModel is MechanicalFitting && ((owner as FamilyInstance).MEPModel as MechanicalFitting).PartType == (PartType)7)
							{
								Parameter parameter = owner.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY);
								if (parameter != null && !parameter.IsReadOnly)
								{
									parameter.Set(0);
								}
							}
						}
					}
				}
			}
			catch
			{
			}
		}

		public static Connector GetConnectConnector(Connector connector)
		{
			Connector result = null;
			if (connector != null && connector.IsConnected)
			{
				ConnectorSetIterator connectorSetIterator = connector.AllRefs.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector2 = obj as Connector;
                    if (connector2 != null && connector2.Owner.UniqueId != connector.Owner.UniqueId && (connector2.ConnectorType == (ConnectorType)1 || connector2.ConnectorType == (ConnectorType)2 || connector2.ConnectorType == (ConnectorType)19))
					{
						result = connector2;
						break;
					}
				}
			}
			return result;
		}
	}
}
